package com.wkhyapp.lm.view;

import android.graphics.Color;
import android.os.Build;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.GrabAlbumAdapter;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.GrabAlbumPresenter;
import com.wkhyapp.lm.contract.GrabAlbumView;
import com.wkhyapp.lm.http.net.AddGoodsRequest;
import com.wkhyapp.lm.http.net.GoodsReq;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class GrabAlbumActivity extends SuperActivity<GrabAlbumPresenter> implements GrabAlbumView {


    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refresh_view;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private GrabAlbumAdapter grabAlbumAdapter;
    private List<Goods> goodses;
    private int mCurrentPage = 1;
    @BindView(R.id.back_iv)
    ImageView back_iv;
    String url;
    @BindView(R.id.qx_rb)
    CheckBox qx_rb;
    @BindView(R.id.bj_rl)
    RelativeLayout bj_rl;
    int goodsNum = 0;
    @BindView(R.id.bj_tv)
    TextView bj_tv;
    boolean isImport = false;
    @Override
    public void initUI() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);
        url = getIntent().getStringExtra("0");
       // url ="https://s.wsxc.cn/IihSdJ";
        goodses = new ArrayList<>();
        grabAlbumAdapter = new GrabAlbumAdapter(R.layout.item_grab_goods,goodses);
        refresh_view.setColorSchemeColors(Color.parseColor("#FFEC48"),Color.parseColor("#FFEC48"));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(grabAlbumAdapter);
        refresh_view.setRefreshing(false);
        mPresenter.getData(url);
        showLoad("");
    }

    @Override
    protected void initListener() {
        grabAlbumAdapter.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.view_index_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return com.chad.library.R.id.load_more_loading_view;
            }
            @Override
            protected int getLoadFailViewId() {
                return com.chad.library.R.id.load_more_load_fail_view;
            }
            @Override
            protected int getLoadEndViewId() {
                return com.chad.library.R.id.load_more_load_end_view;
            }
        });

        refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mPresenter.getData(url);
            }
        });

        grabAlbumAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mCurrentPage = mCurrentPage+1;
                mPresenter.getDataMore(mCurrentPage,url);

            }
        });


        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        qx_rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    grabAlbumAdapter.initCheck(true);
                    int x =0;
                    for (Map.Entry<Integer,Boolean> entry : grabAlbumAdapter.getMap().entrySet()) {
                        if(entry.getValue()){
                            x=x+1;
                        }
                    }

                    bj_tv.setText("一键搬家"+ x+"款");

                }else{
                    bj_tv.setText("一键搬家");
                    grabAlbumAdapter.initCheck(false);
                }
                grabAlbumAdapter.notifyDataSetChanged();
            }
        });


        bj_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int x =0;
                for (Map.Entry<Integer,Boolean> entry : grabAlbumAdapter.getMap().entrySet()) {
                    if(entry.getValue()){
                        x=x+1;
                    }
                }

                if(x>500){
                    TToast("单次搬家最多500款");
                    return;
                }

                if(isImport){
                    TToast("正在导入商品..");
                    return;
                }
                isImport = true;
                AddGoodsRequest request = new AddGoodsRequest();
                List<GoodsReq> list = new ArrayList<GoodsReq>();
                GoodsReq goodsReq = null;
                for (Map.Entry<Integer,Boolean> entry : grabAlbumAdapter.getMap().entrySet()) {
                    if(entry.getValue()){
                        goodsReq = new GoodsReq();
                        goodsReq.setCategoryId(0);
                        goodsReq.setUserId(MemberUtils.getUid());
                        goodsReq.setThirdId(goodses.get(entry.getKey()).getThirdId());
                        if(goodses.get(entry.getKey()).getPrice()!=null){
                            goodsReq.setPrice(goodses.get(entry.getKey()).getPrice().toString());
                        }else{
                            goodsReq.setPrice("0.00");
                        }
                        goodsReq.setDesc(goodses.get(entry.getKey()).getDesc());
                        goodsReq.setImgs(goodses.get(entry.getKey()).getImgs());
                        goodsReq.setGoodsImgs(goodses.get(entry.getKey()).getGoodsImgs());
                        list.add(goodsReq);
                    }
                }
                request.setGoodsReqs(list);
                showLoad("正在导入商品");
                mPresenter.upload(request);
            }
        });
    }

    @Override
    public void setGoods(List<Goods> list) {
        goodses.clear();
        if(list!=null&&list.size()>0){
            goodses.addAll(list);
            grabAlbumAdapter.setData(goodses);
        }
        grabAlbumAdapter.notifyDataSetChanged();
        refresh_view.setRefreshing(false);
        grabAlbumAdapter.loadMoreComplete();
    }

    @Override
    public void setGoodsMore(List<Goods> list) {
        if(list.size()==0){
            grabAlbumAdapter.loadMoreComplete();
            grabAlbumAdapter.loadMoreEnd();
        }else{
            goodses.addAll(list);
            grabAlbumAdapter.notifyDataSetChanged();
            grabAlbumAdapter.loadMoreComplete();
        }
    }

    @Override
    public void upLoadSucc() {
        isImport = false;
        TToast("商品导入成功!");
        dismissLoad();
        showLoad("数据刷新中...");
        finish();

    }


    @Override
    protected void afterCreate() {

    }

    @Override
    protected GrabAlbumPresenter createPresenter() {
        return mPresenter = new GrabAlbumPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_grab_album;
    }


}
